/* 
 * File:   XbeeContainer.h
 * Author: minaev
 *
 * Created on 17 Ноябрь 2013 г., 12:07
 */

#ifndef XBEECONTAINER_H
#define	XBEECONTAINER_H

#include "XshareMem.h"
#include "XUDPShareMem.h"
#include "Xpack.h"
#include "XbeeIf.h"
#include "Xmsg.h"


#include <stdint.h>
#include <pthread.h>
#include <stdio.h>

#define SLN_XCOUNT   0   //count
#define SLN_XADRS    1   //low addresses of xbee modules
#define SLN_XSTAT    2   //status xbee module
#define SLN_XREQSTAT 3   //request change status xbee module
#define SLN_XADC     4   //Xbee adc value
#define SLN_XFIXADC  5   //Xbee adc value
#define SLN_XPINGS   6   //Count ping answers in last ping cycle
#define SLN_XBTNS    7   //status IR-button
#define SLN_XBTNOLDS 8   //status IR-button old
#define SLN_XOLODEV  9   //ololo devs
#define SLN_XLOGNUM  10   //logical number xbeedev

#define SLN_COUNT 11

#define ALARM_DIFF 0x20


#define STAT_POWEROFF   0
#define STAT_IDLE       1
#define STAT_SCAN       2
#define STAT_ALARM      3

class XbeeContainer {
public:
    
    XbeeContainer(XshareMem* shmem, XUDPShareMem* udpmem, XbeeIf* xbeeif);
    virtual ~XbeeContainer();
    int addXbee(uint32_t id);
    void initXbee(uint32_t id);
    uint16_t length();
    int setParam(uint16_t numXbee,uint8_t numParam,void* value);
    void* getParam(uint16_t numXbee,uint8_t numParam);
    int setParamById(uint32_t idXbee,uint8_t numParam,void* value);
    void* getParamById(uint32_t idXbee,uint8_t numParam);
    void stopScan(uint16_t numXbee);
    void startScan(uint16_t numXbee);
    void pinAlarmOn(uint16_t numXbee);
    void pinAlarmOff(uint16_t numXbee);
    
    int findById(uint32_t id);
    bool isAlarm(uint16_t numXbee);
    void sendAlarmMessage(uint16_t numXbee);
    void sendStatusMessage(uint16_t numXbee);
    void sendBtnStatusMessage(uint16_t numXbee);
    void toNewStatus(uint16_t numXbee);
    void pingBroadcast();
    XshareMem* getShareMem();
    XUDPShareMem* getUDPShareMem();
    
private:

    Xpack* pingPack;
    
    Xpack** settingPacks;
    static const uint8_t countsetting=10;
    
    //setting scan mode
    Xpack* analogPin;
    Xpack* digInPin;
    Xpack* digOutOffPin;
    Xpack* digOutOnPin;
    Xpack* changeDetectMode;
    
    
    Xpack* unscanAnalogSample;
    //setting sleep mode
    Xpack* sleepModePack;
    Xpack* noSleepMode;
    Xpack* sleepPeriodPack;
    
    Xpack* awakePeriodPack;
    Xpack* applyChangesPack;
    Xpack* writeChangesPack;
    
    
    Xpack* scanAnalogSample;
    
    Xpack* pullupOffPack;
    
    
    XshareMem* shmem;
    XUDPShareMem* udpmem;
    XbeeIf* xbeeif;
    uint8_t* lenBlocks; //array of length blocks in shared lines
    pthread_mutex_t acsMem;
};

#endif	/* XBEECONTAINER_H */

